#!
# Jomper.Chow
# SmarTask
# 2008-9-16
# task tree panel
#

class TreePanelManager
	
	COLUMN_STATUS,
	COLUMN_COMBO,
	COLUMN_TASK_NAME, 
	COLUMN_START_TIME, 
	COLUMN_DEADLINE, 
	COLUMN_ID , 
	COLUMN_COMMENT ,
	COLUMN_EDITABLE, 
	COMBO_TEXT_COLUMN,
	COMBO_HAS_ENTRY,
	COMBO_TEXT = *(0..10).to_a
  TARGET_TABLE = [["GTK_TREE_MODEL_ROW", 0, 0]]
	
	def initialize uicontext
		@uicontext = uicontext
		@statusModel = [["0",GLib.locale_to_utf8(_("Fresh"))],["1",GLib.locale_to_utf8(_("Ongoing"))],["2",GLib.locale_to_utf8(_("Delay"))],["3",GLib.locale_to_utf8(_("Discard"))],["4",GLib.locale_to_utf8(_("Complete"))]]

		@treeStore = Gtk::TreeStore.new Gdk::Pixbuf,Gtk::ListStore,String,String,String,String,String,TrueClass,Integer,TrueClass,String
		@treeView = Gtk::TreeView.new @treeStore
		@treeView.insert_column -1,"", Gtk::CellRendererPixbuf.new, {:pixbuf => COLUMN_STATUS}
		registerComboEditableColumn GLib.locale_to_utf8(_("Task Status")),COLUMN_COMBO
		registerTextEditableColumn GLib.locale_to_utf8(_("Task Name")),COLUMN_TASK_NAME
		registerTextEditableColumn GLib.locale_to_utf8(_("Start Time")),COLUMN_START_TIME
		registerTextEditableColumn GLib.locale_to_utf8(_("Deadline")),COLUMN_DEADLINE
		registerTextEditableColumn "Id",COLUMN_ID
    @treeView.get_column(COLUMN_ID).visible = false
    registerTextEditableColumn "Comment",COLUMN_COMMENT
    @treeView.get_column(COLUMN_COMMENT).visible = false
    
    @treeView.hover_selection = true
		@treeView.enable_search = true
    
    @treeView.selection.signal_connect("changed") do
      @iter = @treeView.selection.selected
    end
    
	  @treeView.enable_model_drag_source(Gdk::Window::BUTTON1_MASK,TARGET_TABLE,Gdk::DragContext::ACTION_MOVE)
    @treeView.enable_model_drag_dest(TARGET_TABLE, Gdk::DragContext::ACTION_MOVE)
    
    @treeView.signal_connect("cursor-changed") do
      #@iter = @treeView.selection.selected
      @treeView.expand_all
    end
    
    @treeView.signal_connect("drag-end") do |treeView,drag_context|
      puts "new event"
      treeView.model.each do |model, path, iter|
        @uicontext.context.taskManager.getTasks.each do|task|
          if task.id.eql? iter[4]
            if iter.parent != nil 
              task.parentId = iter.parent[4]
            else
              task.parentId = ""
            end
            break
          end
        end
      end
    end
		
	end
	
	def getStatusModel
		@statusModel
	end
	
	def getSelected
    @iter 
	end
  
  def refreshPanel tasks
    @treeStore.clear
    tasks.each {|task|
      if "".eql? task.parentId
        addTask2Panel task,nil
      else
        @treeStore.each {|model, path, iter|
          if iter[4].eql? task.parentId
            addTask2Panel task,iter
            break
          end
        }
      end
    }
  end
	
	def addSubTask2Panel task
	  task.parentId = @uicontext.selected[4] if @uicontext.selected != nil
    addTask2Panel task,@uicontext.selected
	end
	
  def addTask2Panel task,iter
  	iter = @treeStore.append iter
    iter[COLUMN_STATUS] = status2Pixbuf task.status
  	iter[COLUMN_TASK_NAME] = task.name
  	iter[COLUMN_START_TIME] = task.startTime
  	iter[COLUMN_DEADLINE] = task.deadline
  	iter[COLUMN_ID] = task.id
  	iter[COLUMN_COMMENT] = task.comment
    iter[COLUMN_EDITABLE] = true
    iter[COLUMN_COMBO] = @comboModel
    iter[COMBO_TEXT] = @statusModel[task.status.to_i][1]
	  iter[COMBO_HAS_ENTRY] = false
	  iter[COMBO_TEXT_COLUMN] = task.status.to_i  end
  
  #!
  # refresh task model of update
  #
  def refreshTask2Panel task
    @uicontext.selected[COLUMN_STATUS] = status2Pixbuf task.status
    @uicontext.selected[COLUMN_TASK_NAME] = task.name
    @uicontext.selected[COLUMN_START_TIME] = task.startTime
    @uicontext.selected[COLUMN_DEADLINE] = task.deadline
    @uicontext.selected[COLUMN_COMMENT] = task.comment
		@uicontext.selected[COMBO_TEXT_COLUMN] = task.status
  end
  
  def status2Pixbuf status
    return case status.to_s
    when @statusModel[0][0]
      Gdk::Pixbuf.new @uicontext.resourcesPath+"fresh.png", 24, 24
    when @statusModel[1][0]
      Gdk::Pixbuf.new @uicontext.resourcesPath+"ongoing.png", 24, 24
    when @statusModel[2][0]
      Gdk::Pixbuf.new @uicontext.resourcesPath+"delay.png", 24, 24
    when @statusModel[3][0]
      Gdk::Pixbuf.new @uicontext.resourcesPath+"discard.png", 24, 24
    when @statusModel[4][0]
      Gdk::Pixbuf.new @uicontext.resourcesPath+"complete.gif", 24, 24
    end
  end
  
  def removeTask
		@treeStore.remove @uicontext.selected
  end

	def getTreePanel
		@treeView
	end

	def cell_edited(cell, path, text, model)
		iter = model.get_iter Gtk::TreePath.new(path)
		i = iter.path.indices[0]
		case cell.column
		when COLUMN_TASK_NAME
			@uicontext.context.taskManager.getTasks[i].name = text
			iter.set_value cell.column, text
		end
	end
	
	def registerComboEditableColumn comboColumnName,comboColumnIndex
		
		@comboModel = Gtk::ListStore.new String
		@statusModel.each do |status|
			iter = @comboModel.append
			iter[0] = status[1]
		end
		rendererCombo = Gtk::CellRendererCombo.new
		rendererCombo.signal_connect('edited') do |renderer, path, text|
			iter = @treeStore.get_iter(path);
			@uicontext.context.taskManager.getTasks[iter.path.indices[0]].status = @statusModel.rassoc(text)[0]
			iter[COLUMN_STATUS] = status2Pixbuf @statusModel.rassoc(text)[0]
			iter[COMBO_TEXT] = text
			#iter[COMBO_TEXT_COLUMN] = @statusModel.rassoc(text)[0].to_i
		end
		
		@treeView.insert_column -1,comboColumnName, rendererCombo, {:text_column => COMBO_TEXT_COLUMN,
																																:model => comboColumnIndex,
																																:has_entry => COMBO_HAS_ENTRY,
																																:editable => COLUMN_EDITABLE,
																																:text => COMBO_TEXT}
	end
	
	def registerTextEditableColumn columnName,columnIndex
		rendererText = Gtk::CellRendererText.new
    rendererText.signal_connect('edited') do |*args|
      cell_edited(*args.push(@treeView.model))
    end
		
    case columnIndex
    when COLUMN_TASK_NAME
      def rendererText.column
        COLUMN_TASK_NAME
      end
    end
    
    if columnIndex == COLUMN_TASK_NAME
			@treeView.insert_column -1,columnName, rendererText, {:text => columnIndex,:editable => COLUMN_EDITABLE}
		else
			@treeView.insert_column -1,columnName, rendererText, {:text => columnIndex}
    end
	end

end